<template lang="pug">
dialogModel(
    ref="dialogLoad"
    :title="title"
    width="780"
    @confirm="cyGptInfosaveOne"
)
    .cont-wrap
        .cont-li
            .label 服务器地址：
            .input-w
                el-input(v-model="url" placeholder="http://xxx.xxx.xxx.xxx:xxxx")
        .cont-li
            .label 知识库数据集：
            .input-w
                el-input(v-model="dataBaseId")
        .cont-li
            .label API密钥：
            .input-w
                el-input(v-model="gptKey")
        .cont-li
            .label 应用发布标识：
            .input-w
                el-input(v-model="gptAddress")
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dialogModel from '@/components/dialogModel.vue'
import apis from '@/apis'
import { success, confirm, warning } from '@/utils/message'
import { storageGet, storageClear, storageSet } from '@/utils'
const title = ref('AI配置')
const dialogLoad = ref(null)
const open = () =>{
    dialogLoad.value.open()
    let obj = storageGet('cyGptInfosaveOne')
    if(obj){
        url.value = obj.url
        dataBaseId.value = obj.dataBaseId
        gptKey.value = obj.gptKey
        gptAddress.value = obj.gptAddress
    }else{
        cyGptInfoqueryAllNotPage()
    }
}
const url = ref('')
const dataBaseId = ref('')
const gptKey = ref('')
const gptAddress = ref('')
//保存
const cyGptInfosaveOne = () => {
    if(url.value && !/^http:\/\/\d+\.\d+\.\d+\.\d+:\d+\/?$/.test(url.value)){
        warning('请输入正确的服务器地址:http://xxx.xxx.xxx.xxx:xxxx')
        return
    }
    let obj = {
        url: url.value,
        dataBaseId: dataBaseId.value,
        gptKey: gptKey.value,
        gptAddress: gptAddress.value
    }
    apis.cyGptInfosaveOne(obj).then(res => {
        if(res.status == 1){
            success(res.msg)
            dialogLoad.value.close()
            storageSet('cyGptInfosaveOne', obj)
            window.dataInfoInfo && window.dataInfoInfo()
        }
    })
}
//获取
let dataInfo = {}
const cyGptInfoqueryAllNotPage = () => {
    apis.cyGptInfoqueryAllNotPage().then(res => {
        if(res.status == 1){
            if(res.data && res.data[0]){
                dataInfo = res.data[0]
                url.value = dataInfo.url
                dataBaseId.value = dataInfo.dataBaseId
                gptKey.value = dataInfo.gptKey
                gptAddress.value = dataInfo.gptAddress
            }

        }
    })
}
defineExpose({
    open
})

</script>

<style lang="less" scoped>
.cont-wrap{
    height: 180px;
    .cont-li{
        display: flex;
        align-items: center;
        line-height: 40px;
        .label{
            width: 100px;
            text-align: right;
            margin-right: 5px;
        }
        .input-w{
            flex: 1
        }
    }
}
</style>